//
//  ChangepopView.m
//  quxiangtao
//
//  Created by HHuajiang on 2018/11/27.
//  Copyright © 2018 shanhaijin. All rights reserved.
//

#import "ChangepopView.h"

@interface ChangepopView ()

@property (nonatomic, weak) UITextField *textfield;
@property (nonatomic, weak) UILabel *doslabel;
@property (nonatomic, weak) UILabel *numLabel;

@end

@implementation ChangepopView

- (instancetype)init {
    if (self = [super init]) {
        self.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.6];
        
        UIView *backgroundView = [[UIView alloc] init];
        backgroundView.backgroundColor = WhiteColor;
        [self addSubview:backgroundView];
        [backgroundView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.center.equalTo(self);
            make.size.mas_equalTo(CGSizeMake(Scale_W(275), Scale_W(235)));
        }];
        MKViewBorderRadius(backgroundView, 2, 0, WhiteColor);
       
        UILabel *titleLabel = [[UILabel alloc] init];
        titleLabel.font = Font(30);
        titleLabel.textColor = Color333333;
        titleLabel.text = @"兑换比例";
        [titleLabel setContentHuggingPriority:UILayoutPriorityRequired forAxis:UILayoutConstraintAxisHorizontal];
        [backgroundView addSubview:titleLabel];
        [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(Scale_W(20));
            make.top.mas_equalTo(Scale_W(40));
        }];
        
        UITextField *fieldtext = [[UITextField alloc] init];
        fieldtext.placeholder = @"请输入比例";
        fieldtext.keyboardType = UIKeyboardTypeNumberPad;
        fieldtext.font = Font(30);
        fieldtext.textAlignment = NSTextAlignmentCenter;
        [fieldtext setValue:Font(30) forKeyPath:@"_placeholderLabel.font"];
        [backgroundView addSubview:fieldtext];
        [fieldtext mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.mas_equalTo(-Scale_W(20));
            make.height.mas_equalTo(Scale_W(30));
            make.centerY.equalTo(titleLabel.mas_centerY);
            make.left.equalTo(titleLabel.mas_right).offset(Scale_W(10));
        }];
        MKViewBorderRadius(fieldtext, 5, 0.5, Color999999);
        
        UILabel *labelshow = [[UILabel alloc] init];
        labelshow.textColor = Color999999;
        labelshow.font = Font(24);
        [labelshow setContentHuggingPriority:UILayoutPriorityRequired forAxis:UILayoutConstraintAxisVertical];//设置huggingPriority
        [backgroundView addSubview:labelshow];
        [labelshow mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(fieldtext.mas_left);
            make.top.equalTo(fieldtext.mas_bottom).offset(Scale_W(20));
        }];
        
        UIView *color333View = [[UIView alloc] init];
        color333View.backgroundColor = Colordddddd;
        [backgroundView addSubview:color333View];
        [color333View mas_makeConstraints:^(MASConstraintMaker *make) {
            make.height.mas_equalTo(Scale_W(60));
            make.top.equalTo(labelshow.mas_bottom).offset(Scale_W(20));
            make.right.left.mas_equalTo(0);
        }];
        
        UILabel *numLabel = [[UILabel alloc] init];
        numLabel.backgroundColor = Colordddddd;
        numLabel.text = @"3.102";
        numLabel.numberOfLines = 0;
        numLabel.textColor = [UIColor redColor];
        numLabel.font = Font(24);
        numLabel.textAlignment = NSTextAlignmentCenter;
        [color333View addSubview:numLabel];
        [numLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.mas_equalTo(0);
            make.bottom.equalTo(color333View.mas_centerY).offset(-2);
        }];

        UILabel *bililabel = [[UILabel alloc] init];
        bililabel.backgroundColor = Colordddddd;
        bililabel.text = @"当前平均兑换比例";
        bililabel.numberOfLines = 0;
        bililabel.textAlignment = NSTextAlignmentCenter;
        bililabel.font = Font(24);
        bililabel.textColor = Color999999;
        [color333View addSubview:bililabel];
        [bililabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.mas_equalTo(0);
            make.top.equalTo(color333View.mas_centerY).offset(2);
        }];
        
        UIButton *sureButton = [UIButton buttonWithType:UIButtonTypeCustom];
        sureButton.backgroundColor = Color333333;
        [sureButton setTitle:@"立即修改" forState:UIControlStateNormal];
        sureButton.titleLabel.font = Font(32);
        [sureButton setTitleColor:WhiteColor forState:UIControlStateNormal];
        [backgroundView addSubview:sureButton];
        [sureButton mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.bottom.mas_equalTo(0);
            make.bottom.mas_equalTo(0);
            make.height.mas_equalTo(Scale_W(60));
        }];
        [sureButton addTarget:self action:@selector(sureButtonClick) forControlEvents:UIControlEventTouchUpInside];
        
        UIButton *deleteButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [deleteButton setImage:[UIImage imageNamed:@"deleteH"] forState:UIControlStateNormal];
        [deleteButton setTintColor:WhiteColor];
        [self addSubview:deleteButton];
        [deleteButton mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.equalTo(self.mas_centerX);
            make.size.mas_equalTo(CGSizeMake(Scale_W(25), Scale_W(25)));
            make.top.equalTo(backgroundView.mas_bottom).offset(Margin);
        }];
        [deleteButton addTarget:self action:@selector(closuttonClick) forControlEvents:UIControlEventTouchUpInside];
        
        self.textfield = fieldtext;
        self.numLabel = numLabel;
        self.doslabel = labelshow;
        
    }
    return self;
}

- (void)setModel:(ChangingModel *)model {
    self.doslabel.text = model.doc;
}

- (void)setDicationary:(NSDictionary *)dicationary {
    self.numLabel.text = [NSString stringWithFormat:@"%@",dicationary[@"scale"]];
}

- (void)closuttonClick {
    [self dissMissPoPview];
}

- (void)sureButtonClick {
    
    if (self.textfield.text.length < 1) {
        [[KYAlertView sharedInstance] showAlertViewWithMessage:@"请输入比例"];
        return;
    }
    if ([self.textfield.text floatValue] <= 0) {
        [[KYAlertView sharedInstance] showAlertViewWithMessage:@"请输入正确的比例"];
        return;
    }
    if (self.sureClickBlock) {
        self.sureClickBlock(self.textfield.text);
        [self dissMissPoPview];
    }
}


- (void)dissMissPoPview {
    [self removeFromSuperview];
}

- (void)showPoPview {
    [[UIApplication sharedApplication].keyWindow addSubview:self];
    [self mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.top.bottom.mas_equalTo(0);
    }];
}

@end
